
package main.Link;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Locale;

public class LTETableModel extends AbstractTableModel {

    private String[] columnNames = {"?", "Действие", "ID", "Наименование работы", "№", "Наименование сметной строки", "Кол-во по сметной строке", "Ед. изм.", "K", "K общ", "ФО", "Основной физ. объем", "Ед. изм."};
    private ArrayList<SQLLink> data = new ArrayList<>();
    Class[] types = new Class[]{
            Boolean.class, String.class, String.class, String.class, String.class, String.class, Double.class, String.class, Double.class, Double.class, Boolean.class, Double.class, String.class
    };
    private JDialog parentFrame;

    @Override
    public int getColumnCount() { return columnNames.length; }

    @Override
    public int getRowCount() { return data.size(); }

    @Override
    public String getColumnName(int col) { return columnNames[col]; }

    public int getSize() {
        return data.size();
    }

    @Override
    public Object getValueAt(int row, int col) {
        SQLLink link = data.get(row);

        DecimalFormat df = new DecimalFormat();
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
        otherSymbols.setDecimalSeparator('.');
        otherSymbols.setGroupingSeparator(' ');

        df.setMaximumFractionDigits(6);
        df.setMinimumFractionDigits(0);
        df.setGroupingUsed(false);
        df.setDecimalFormatSymbols(otherSymbols);


        switch( col )
        {
            case 0: return link.getAsk();
            case 1: return link.getAction();
            case 2: return link.getTaskShortName();
            case 3: return link.getTaskName();
            case 4: return link.getSmrNum();
            case 5: return link.getSmrName();
            case 6: return df.format(link.getSmrVol());
            case 7: return link.getSmrUnit();
            case 8: return df.format(link.getFact());
            case 9: return df.format(link.getFactFull());
            case 10: return link.getPv();
            case 11:
                if ((boolean) getValueAt(row, 10)) return df.format(link.getPvVol());
                else return null;
            case 12:
                if ((boolean) getValueAt(row, 10)) return link.getUnit();
                else return null;
            default: return "Unknown";
        }
    }

    public ArrayList<SQLLink> getData() { return data; }

    public int getTaskObjId(int row) {
        return data.get(row).getTaskId();
    }

    public int getSmRowId(int row) {
        return data.get(row).getSmrId();
    }

    public String getSmrMapId(int row) {
        return data.get(row).getSmrMapId();
    }

    public SQLLink getLink(int row) {
        return data.get(row);
    }

    public BigDecimal getFactFull(int row) {
        return data.get(row).getFactFull();
    }

    boolean[] canEdit = new boolean[]{true, false, false, false, false, false, false, false, true, true, true, true, false};

    @Override
    public Class getColumnClass(int columnIndex) {
        return types[columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex != 11) {
            if (columnIndex != 10) {
                return canEdit[columnIndex];
            } else {
                SQLLink link = data.get(rowIndex);
                return !link.getPvType().equals("") && canEdit[columnIndex];
            }
        }
        return (boolean) getValueAt(rowIndex, 10);
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        if (data.size() > row) {
            SQLLink link = data.get(row);

            //System.out.println ( "value = " + value + "        | row = " + row + "        | col = " + col );

            switch( col )
            {
                case 0:
                    link.setAsk((Boolean) value);
                    break;
                case 1:
                    link.setAction((String) value);
                    break;
                case 2:
                    link.setTaskShortName((String) value);
                    break;
                case 3:
                    link.setTaskName((String) value);
                    break;
                case 4:
                    link.setSmrNum(value.toString());
                    break;
                case 5:
                    link.setSmrName((String) value);
                    break;
                case 6:
                    link.setSmrVol(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    break;
                case 7:
                    link.setSmrUnit((String) value);
                    break;
                case 8:
                    link.setFact(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    break;
                case 9:
                    link.setFactFull(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    break;
                case 10:
                    link.setPv((Boolean) value);
                    break;
                case 11:
                    link.setPvVol(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    System.out.println("link pvVol = " + link.getPvVol());
                    break;
                case 12:
                    link.setUnit((String) value);
                    break;

            }

        } else {

            SQLLink link = new SQLLink();

            switch( col )
            {
                case 0:
                    link.setAsk((Boolean) value);
                    break;
                case 1:
                    link.setAction((String) value);
                    break;
                case 2:
                    link.setTaskShortName((String) value);
                    break;
                case 3:
                    link.setTaskName((String) value);
                    break;
                case 4:
                    link.setSmrNum(value.toString());
                    break;
                case 5:
                    link.setSmrName((String) value);
                    break;
                case 6:
                    link.setSmrVol(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    break;
                case 7:
                    link.setSmrUnit((String) value);
                    break;
                case 8:
                    link.setFact(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    break;
                case 9:
                    link.setFactFull(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    break;
                case 10:
                    link.setPv((Boolean) value);
                    break;
                case 11:
                    link.setPvVol(new BigDecimal((Double) value).setScale(6, RoundingMode.HALF_UP));
                    break;
                case 12:
                    link.setUnit((String) value);
                    break;
            }

            data.add(link);
        }
        fireTableCellUpdated(row, col);
    }

    public JDialog getParentFrame() {
        return parentFrame;
    }

    public void setParentFrame(JDialog parentFrame) {
        this.parentFrame = parentFrame;
    }
}